﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using iShoppingBUS;
using iShoppingDTO;

namespace iShopping.Member
{
    public partial class SanPhamThang : System.Web.UI.Page
    {
        public static int CurrentPage = 0;
        List<SanPhamDTO> sanPhamDtos = new List<SanPhamDTO>();
        private int countPage = 0;
        protected void Page_PreInit(object sender, EventArgs e)
        {
            GiaoDienDTO giaoDienDto = GiaoDienBUS.SelectGiaoDienMa("Theme001");
            if (giaoDienDto != null)
            {
                this.Theme = giaoDienDto.TenTheme;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session.Count > 0)
            {
                string mataikhoan = this.Session["userid"].ToString().Trim();

                if (!IsPostBack)
                {
                    TaiKhoanBUS taiKhoanBus = new TaiKhoanBUS();
                    ThongTinTaiKhoanBUS thongTinTaiKhoanBus = new ThongTinTaiKhoanBUS();

                    DataTable dt = taiKhoanBus.LoaiTaiKhoan(mataikhoan);
                    string images = dt.Rows[0]["Avatar"].ToString().Trim();
                    if (images == "")
                    {
                        dt.Rows[0]["Avatar"] = "~/images/user.png";
                    }
                    FormView2.DataSource = dt;
                    FormView2.DataBind();

                    FormView3.DataSource = dt;
                    FormView3.DataBind();

                    DataBindDataList();
                    DataBindDataListProductsByCategory();
                }

               
            }
            else
            {
                Response.Redirect("/Guest/LoginPage.aspx");
            }

        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string page = Request.QueryString["pgn"];
            int pgns = 0;
            Int32.TryParse(page, out pgns);
            Response.Redirect("~/Member/SanPhamThang.aspx?pgn=" + (pgns - 1));
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            string page = Request.QueryString["pgn"];
            int pgns = 0;
            Int32.TryParse(page, out pgns);
            Response.Redirect("~/Member/SanPhamThang.aspx?pgn=" + (pgns + 1));

        }

        private void DataBindDataList()
        {
           string mataikhoan = this.Session["userid"].ToString().Trim();
            sanPhamDtos = SanPhamBUS.SelectSPOfTrangThai(mataikhoan, "Thành Công");

            // phan trang
            countPage = sanPhamDtos.Count / 3;
            if (sanPhamDtos.Count % 3 != 0)
            {
                countPage = sanPhamDtos.Count / 3 + 1;
            }


            for (int i = 0; i < countPage; i++)
            {
                HyperLink hlNumberPages = new HyperLink();
                LiteralControl literal = new LiteralControl("&nbsp;&nbsp;");
                hlNumberPages.Text = (i + 1).ToString();
                hlNumberPages.NavigateUrl = string.Format("~/Member/SanPhamThang.aspx?pgn=" + (i + 1));
                Panel1.Controls.Add(hlNumberPages);
                Panel1.Controls.Add(literal);

            }
        }

        private void DataBindDataListProductsByCategory()
        {
            string page = Request.QueryString["pgn"];
            int pgn = 0;
            Int32.TryParse(page, out pgn);

            List<SanPhamDTO> sanphamDTO = GetSanPhamListForPage(pgn - 1, 3, sanPhamDtos);
            if (sanphamDTO.Count > 0)
            {
                // Thiet lap phan trang
                //PagedDataSource pds = new PagedDataSource();
                //pds.AllowPaging = true;
                //pds.DataSource = sanPhamDtos;
                //pds.PageSize = 3;
                //pds.CurrentPageIndex = CurrentPage;

                DataList1.DataSource = sanphamDTO;
                DataList1.DataBind();


                //Hien Prev hoac Next button neu nhu CurrentPage o trang dau hoac cuoi
                if (pgn == 1)
                {
                    lbTruoc.Visible = false;
                }
                else
                {
                    lbTruoc.Visible = true;
                }
                if (pgn == countPage)
                {
                    lbSau.Visible = false;
                }
                else
                {
                    lbSau.Visible = true;
                }

            }
            else
            {
                lbSau.Visible = false;
                lbTruoc.Visible = false;
            }

        }
        private List<SanPhamDTO> GetSanPhamListForPage(int pageIdx, int pageSize, List<SanPhamDTO> srcList)
        {
            int start = pageIdx * pageSize;
            int end = start + pageSize;
            if (end > srcList.Count)
            {
                end = srcList.Count;
            }

            List<SanPhamDTO> slicedList = new List<SanPhamDTO>();
            for (int i = start; i < end; i++)
            {
                slicedList.Add(srcList[i]);
            }
            return slicedList;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Button imageButton = (Button)sender;
            for (int i = 0; i < DataList1.Items.Count; i++)
            {
                Button iButton = (Button)DataList1.Items[i].FindControl("Button1");
                if (imageButton.ClientID == iButton.ClientID)
                {
                    Response.Redirect("~/Member/SamPhamThanhCong.aspx?id=" + iButton.CommandName.Trim());
                }
                //ImageButton imageButton = (ImageButton)DataList1.Items[i].FindControl("ImageButton1");
                //imageButton.Click += new ImageClickEventHandler(imageButton_Click);

            }
        }
    }
}
